package com.zgw.his.design.model.widgets;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.e4.xwt.forms.medicalControl.IMedicalControl;
import org.eclipse.wb.core.model.ObjectInfo;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.xml.model.EditorContext;
import org.eclipse.wb.internal.core.xml.model.creation.CreationSupport;
import org.eclipse.wb.internal.core.xml.model.description.ComponentDescription;
import org.eclipse.wb.internal.core.xml.model.property.GenericPropertyImpl;
import org.eclipse.wb.internal.xwt.model.widgets.CompositeInfo;
import org.eclipse.wb.internal.xwt.model.widgets.ControlInfo;

import com.zgw.his.common.ValueUtil;
import com.zgw.his.design.common.IdentifyHelp;

/**
 * Medical 控件模型
 * */
public class MedicalControlInfo extends ControlInfo {
	public final static String PROPERTY_CODE = "code";
	public final static String CODE_PREFIX = "C_";

	public MedicalControlInfo(EditorContext context,
			ComponentDescription description, CreationSupport creationSupport)
			throws Exception {
		super(context, description, creationSupport);
	}

	static int i = 0;

	// TODO 设置默认的 标识码
	@Override
	protected void refresh_afterCreate() throws Exception {
		super.refresh_afterCreate();
		try {
			if (!ValueUtil.hasValue(getCode())) { // 等于空的情况下才进行赋值
				System.out.println(i++);
				Long l = new Date().getTime();
				setDefaultCode();
				System.out.println("用时:" + (new Date().getTime() - l));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// if (i == 0) {
		// i = 1;
		// startEdit();
		// for (MedicalControlInfo c : getRoot().getChildren(
		// MedicalControlInfo.class)) {
		// String bou = c.getElement().getAttribute("bounds");
		// String[] bound = bou.split(",");
		// bound[1] = (Integer.parseInt(bound[1].trim()) - 2) + "";
		// c.getElement().setAttribute(
		// "bounds",
		// bound[0] + "," + "" + bound[1] + "," + "" + bound[2]
		// + "," + "" + bound[3]);
		// }
		// endEdit();
		// }
	}

	// 设置默认 code
	private void setDefaultCode() throws Exception {
		List<Long> suffixs = new ArrayList<Long>();
		List<MedicalControlInfo> children = getAllControls(
				new ArrayList<MedicalControlInfo>(), getRoot());

		String code;
		String codeSuffix;
		for (MedicalControlInfo c : children) {
			code = c.getCode();
			if (code != null && code.startsWith(CODE_PREFIX)) {
				codeSuffix = code.substring(CODE_PREFIX.length());
				if (codeSuffix.matches("[0-9]+")) {
					suffixs.add(Long.parseLong(codeSuffix));
				}
			}
		}
		Long newCode = IdentifyHelp.getMinExceptList(suffixs
				.toArray(new Long[suffixs.size()]));
		setCode(CODE_PREFIX + newCode);
	}

	protected String getCode() throws Exception {
		String result = null;
		for (Property p0 : getProperties()) {
			if (p0 instanceof GenericPropertyImpl
					&& ((GenericPropertyImpl) p0).getDescription().getName()
							.equals(PROPERTY_CODE)) {
				if (p0.getValue() != null) {
					result = p0.getValue().toString();
				}
				break;
			}
		}
		return result;
	}

	protected void setCode(String code) throws Exception {
		Property p = null;
		for (Property p0 : getProperties()) {
			if (p0 instanceof GenericPropertyImpl
					&& ((GenericPropertyImpl) p0).getDescription().getName()
							.equals(PROPERTY_CODE)) {
				p = p0;
				break;
			}
		}
		if (p != null)
			p.setValue(code);
	}

	private boolean isMedical() {
		Class<?> c = getDescription().getComponentClass();
		return IMedicalControl.class.isAssignableFrom(c);
		// for (Class cl : c.getInterfaces()) {
		// if (cl.equals(IMedicalControl.class))
		// return true;
		// }
		// return false;
	}

	// 获得所有控件
	private List<MedicalControlInfo> getAllControls(
			List<MedicalControlInfo> result, ObjectInfo obj) {
		for (ObjectInfo o : obj.getChildren()) {
			if (o instanceof CompositeInfo) {
				getAllControls(result, (CompositeInfo) o);
			}
		}
		result.addAll(obj.getChildren(MedicalControlInfo.class));
		return result;
	}
}
